package communicationMessages;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Vector;
import java.util.logging.Level;

import data.Forum;
import data.Member;
import data.MemberDataBase;
import database.DB;


import logger.LoggerWriter;

public class AddSubForumMessage extends Message {

	@Override
	public String getAnswer() {
		String[] arr = this.content.split(delimiter);
		LoggerWriter logger = LoggerWriter.getLoggerWriter();
		String ans = "";
		if (arr.length != 2)
		{
			logger.log("failed"+delimiter+"add SubForum wrong format"+delimiter, Level.WARNING);
			ans = "failed"+delimiter+"add SubForum wrong format"+delimiter;
		}
		else
		{
			try
			{
				String userName , subForumName;
				userName = arr[0];
				subForumName = arr[1];
				if(DB.isAdmin(userName)&& !DB.isSubForumExistBySubForumName(subForumName)){
					int userID = DB.getUserId(userName);
					String subForumIndex = DB.addSubForum(subForumName,userID);
					ans = "addedSubForum"+delimiter + "admin"+delimiter+"Success"+delimiter;
					ans+= subForumIndex+delimiter+"CandidatesModeratorsList"+delimiter;
					ans = getCandidatesName(ans);
				}	
			}
			catch(SQLException e)
			{
				System.out.println("AddSubForumMessage SQLException " + e.getMessage());
			}
		}
		return ans;
	}

	private String getCandidatesName(String ans) throws SQLException {
		ResultSet res = DB.getAllUsers();
		while (res.next())
		{
			ans+=res.getString(DB.userTable.userName.ordinal()) + delimiter;
		}
		return ans;
	}
	
//	Forum forum = Forum.getForum();
//	if(forum.isPremitedToAddSubForum(userName)){
//		int subForumIndex = forum.addSubForum(subForumName, forum.get_admin());
//		ans = "addedSubForum"+delimiter + MemberDataBase.getUserRoll(userName)+delimiter+"Success"+delimiter;
//		ans+= subForumIndex+delimiter+"CandidatesModeratorsList"+delimiter;
//		Vector<Member> candidates =forum.getPossibleModeratorForNewForum();
//		for (Member moderator : candidates) {
//			ans+=moderator.get_name()+delimiter;
//		}
//	}	

}
